package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.MerchantPaidFee;
import com.turing.post.organization.util.MerchantPaidFeeDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.MerchantPaidFeeDaoImpl")
@Remote
@Local
public class MerchantPaidFeeDaoImpl implements MerchantPaidFeeDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public MerchantPaidFee getMerchantPaidFeeByKey(String merchantNo,
			String businessType) {
		MerchantPaidFee mpf = new MerchantPaidFee();
		mpf.setMerchantNo(merchantNo);
		mpf.setBusinessType(businessType);
		return _em.find(MerchantPaidFee.class, mpf);
	}

	@SuppressWarnings("unchecked")
	public List<MerchantPaidFee> getMerchantPaidFeeListByMerchantNo(
			String merchantNo) {
		Query query = _em
				.createNativeQuery(
						"select t.* from MerchantPaidFee t where t.merchantNo = ?1 order by t.businessType",
						MerchantPaidFee.class).setParameter(1, merchantNo);
		return query.getResultList();
	}

	public void insert(MerchantPaidFee mpf) throws PCPSApplicationException {
		GeneralDao.insert(_em, mpf);
	}

	public void remove(MerchantPaidFee mpf) throws PCPSApplicationException {
		MerchantPaidFee origmpf = getMerchantPaidFeeByKey(mpf.getMerchantNo(),
				mpf.getBusinessType());
		if (origmpf != null)
			GeneralDao.remove(_em, origmpf);
	}

	public void update(MerchantPaidFee mpf) throws PCPSApplicationException {
		GeneralDao.update(_em, mpf);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantPaidFee> getMerchantPaidFeeList(PCPSWebInput webInput,
			String merchantNo, String businessType, String businessName,
			Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String sql = "";
		// String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		sql = "select t.* from MerchantPaidFee t where t.MerchantNo = '"
				+ merchantNo
				+ "' and (?1 is null or businessType=?1) and (?2 is null or businessName=?2) order by merchantNo";
		Query query = _em.createNativeQuery(sql, MerchantPaidFee.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, businessType);
		query.setParameter(2, businessName);
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantPaidFeeTotal(PCPSWebInput webInput,
			String merchantNo, String businessType, String businessName) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return BigInteger.ZERO;
		}
		String sql = "";
		// String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		sql = "select nvl(count(*),0) from MerchantPaidFee t where t.MerchantNo = '"
				+ merchantNo
				+ "' and (?1 is null or businessType=?1) and (?2 is null or businessName=?2) order by merchantNo";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessType);
		query.setParameter(2, businessName);
		return new BigInteger(query.getSingleResult().toString());
	}
}